package org.csu.code.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Category;
import org.csu.code.api.entity.Item;
import org.csu.code.api.entity.Product;
import org.csu.code.api.persistence.CategoryMapper;
import org.csu.code.api.persistence.ItemMapper;
import org.csu.code.api.persistence.ProductMapper;
import org.csu.code.api.service.CatalogService;
import org.csu.code.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private Item item1;

    @Override
    //获取农产品大类service
    public CommonResponse<List<Category>> getCategoryList() {
        List<Category> categoryList = categoryMapper.selectList(null);
        //若没有查到不会返回null，而是长度为0
        if(categoryList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    //获取指定某农产品大类Category信息service
    public CommonResponse<Category> getCategory(String categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        if(category == null)
        {
            return CommonResponse.createForSuccessMessage("没有该ID的Category");
        }
        return CommonResponse.createForSuccess(category);
    }

    @Override
    //获取指定某农产品大类Category下的细分农产品Product列表
    public CommonResponse<List<Product>> getProductListByCategoryId(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);

        List<Product> productList = productMapper.selectList(queryWrapper);
        if(productList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该分类下没有Product子分类");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    //获取指定某细分农产品Product信息
    public CommonResponse<Product> getProductById(String productId) {
        Product product = productMapper.selectById(productId);
        if (product == null){
            return CommonResponse.createForError("没有该product的信息");
        }
        return CommonResponse.createForSuccess(product);
    }

    @Override
    //获取指定细分农产品Product下的商品item列表（农产品详情）
    public CommonResponse<List<ItemVO>> getItemsByProductId(String productId){
        //查item表获取对应item列表
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        queryWrapper.eq("status",1);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForError("该product下没有item商品");
        }

        //查product表获取对应product
        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();

        for(Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
    }

    @Override
    //农产品item详细信息（农产品详情）
    public CommonResponse<ItemVO> getItemById(String itemId) {
//        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("itemid",itemId);
//        Item item = itemMapper.selectById(queryWrapper);
        Item item = itemMapper.selectById(itemId);
        if(item==null){
            return CommonResponse.createForError("没有该item的信息");
        }

        Product product = productMapper.selectById(item.getProductId());
        ItemVO itemVO = itemToItemVO(item,product);
        return CommonResponse.createForSuccess(itemVO);
    }


    @Override
    //上架农产品
    //status=0:待审核 1:上架 2:下架
    public CommonResponse<String> insertItem(Item item){
        //寻找是否已经上架过
        int rows = itemMapper.selectCount(Wrappers.<Item>query().eq("name",item.getName()));
        if(rows > 0){
            item1 = itemMapper.selectOne(Wrappers.<Item>query().eq("name",item.getName()));
            if (item1.getStatus() == 1){
                return CommonResponse.createForError("提交失败");
            }
            item1.setStatus(0);
            itemMapper.updateById(item1);
        }
        else {
            //判断id
            int row = itemMapper.selectCount(null);
            //设置为待审核状态
            item.setItemId(++row);
            item.setStatus(0);
            itemMapper.insert(item);
        }
//        item = itemMapper.selectById(item.getItemId());
//        if (item.getStatus() != 0){
//            return CommonResponse.createForError("提交失败");
//        }

        return  CommonResponse.createForSuccess("提交成功");

    }

    @Override
    //农产品item下架
    public CommonResponse<String> deleteItem(int itemId){
        int rows = itemMapper.selectCount(Wrappers.<Item>query().eq("itemid",itemId));
        if (rows<=0){
            return CommonResponse.createForError("下架失败！");
        }
        Item item = itemMapper.selectById(itemId);
        //下架的status为2
        item.setStatus(2);
        itemMapper.updateById(item);
//        itemMapper.insert(item);

        if (itemMapper.selectById(itemId).getStatus() != 2){
            return CommonResponse.createForError("下架失败！");
        }
        return  CommonResponse.createForSuccess("下架成功！");
    }


    private ItemVO itemToItemVO(Item item,Product product){
        //加入item
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
//        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
//        itemVO.setAttribute4(item.getAttribute4());
//        itemVO.setAttribute5(item.getAttribute5());
        itemVO.setName(item.getName());
        itemVO.setStock(item.getStock());
        itemVO.setScore(item.getScore());
        itemVO.setDescription(item.getDescription());
//        itemVO.setWeight(item.getWeight());
//        itemVO.setServe(item.getServe());
        itemVO.setSales(item.getSales());
        itemVO.setLocation(item.getLocation());

        //加入product
        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        return itemVO;
    }

    public CommonResponse<List<Product>> searchProduct(String keyword){
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.like("name",keyword);
        List<Product> products = productMapper.selectList(productQueryWrapper);
        return CommonResponse.createForSuccess(products);
    }

    public CommonResponse<List<Item>> searchItem(String keyword){
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.like("name",keyword);
        List<Item> items = itemMapper.selectList(itemQueryWrapper);
        return CommonResponse.createForSuccess(items);
    }

    public CommonResponse<List<Category>> searchCategory(String keyword){
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.like("name",keyword);
        List<Category> categories = categoryMapper.selectList(categoryQueryWrapper);
        return CommonResponse.createForSuccess(categories);
    }
}
